package util;

import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;

import util.*;

import board.*;

public class SiteMap extends Action{

	public ActionForward execute(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		SiteMapForm smForm = (SiteMapForm)form;
				
		Vector<BoardNames> freeV = new Vector<BoardNames>();
		freeV = this.getBoardMenus("freeboard", "fbname", 2);
		smForm.setFreeV(freeV);
		
		Vector<BoardNames> dataV = new Vector<BoardNames>();
		dataV = this.getBoardMenus("databoard", "dbname", 7);
		smForm.setDataV(dataV);
	
		Vector<BoardNames> mainV = new Vector<BoardNames>();
		mainV = this.getMainBoard();
		smForm.setMainV(mainV);
		
		Vector<BoardNames> photoV = new Vector<BoardNames>();
		photoV = this.getBoardMenus("photoboard", "pbname", 4);
		smForm.setPhotoV(photoV);
		
		return mapping.findForward("ok");
	}
	
	public static Vector getBoardMenus(String board, String bname, int cono) {
		 	
		Vector<BoardNames> v = new Vector<BoardNames>();
		String[] bnames = null;
		String url = null;
		if(board.equals("photoboard")){
			url = board+".do?method=photoList&cono="+cono+"&"+bname+"=";
		}else{
			url = board+".do?method=getList&cono="+cono+"&"+bname+"=";
		}
		
		if(board.equals("freeboard")){
			FreeBoardDAO fbDAO = new FreeBoardDAO();
			bnames = fbDAO.getBoardMenus(cono);
		}else if(board.equals("databoard")){
			DataBoardDAO dbDAO = new DataBoardDAO();
			bnames = dbDAO.getBoardMenus(cono);
		}else if(board.equals("photoboard")){
			PhotoBoardDAO dbDAO = new PhotoBoardDAO();
			bnames = dbDAO.getBoardMenus(cono);
		}
		
		for(int i=0;i<bnames.length;i++){
			BoardNames bns = new BoardNames();
			bns.setBname(bnames[i]);
			bns.setUrl(url);
			v.add(bns);
		}		
		return v;
	}
	
	public static Vector getMainBoard() {
		
		Vector<BoardNames> mainV = new Vector<BoardNames>();
		
		BoardNames nbns1 = new BoardNames();
		nbns1.setBname("공지사항");
		nbns1.setUrl("noticeboard.do?method=getList&cono=6&nbname=");
		mainV.add(nbns1);
		
		BoardNames nbns2 = new BoardNames();
		nbns2.setBname("ITEA 뉴스");
		nbns2.setUrl("noticeboard.do?method=getList&cono=8&nbname=");
		mainV.add(nbns2);		
		
//		자유게시판 세팅
		BoardNames fbns = new BoardNames();
		FreeBoardDAO fbDAO = new FreeBoardDAO();
		String[] fbnames = fbDAO.getBoardMenus(5);
		String furl = "freeboard.do?method=getList&cono=5&fbname=";
		for(String fb : fbnames){
			fbns.setBname(fb);
			fbns.setUrl(furl);
			mainV.add(fbns);			
		}
		
//		QnA 게시판 세팅
		BoardNames qbns = new BoardNames();
		qbns.setBname("QnA 게시판");
		qbns.setUrl("qnaboard.do?method=list&qbname=");
		
		mainV.add(qbns);
				
		return mainV;	
	}
	
}
